#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

#include <draw.h>
#include <encoding_manager.h>
#include <disp_manager.h>


// 打开txt文本文件之后根据bom判断txt文件编码格式，然后除掉bom，就找到了真正开始显示文本内容的位置

// 打开txt文本文件之后根据bom判断txt文件编码格式，然后除掉bom，就找到了真正开始显示文本内容的位置
static int textfile_fd;
static unsigned char *textfilebuf_start; // txt文件起始地址
static unsigned char *textfilebuf_end;

static unsigned char *textfile_first_char_pos; // 除掉bom后txt文件中第一个字符的位置
static unsigned char *textfile_next_char_pos; 

static PEncodingOpr textfile_EncodingOpr; // txt文件的编码操作结构体
static DispOpr textfile_DispOpr; // txt文件的显示操作结构体
static FontOpr textfile_FontOpr; // txt文件的字体操作结构体

static unsigned int first_char_unicode;
static PFontBitMap pt_first_char_fontbitmap;
static unsigned int textfilt_fontsize;


// 打开txt文件，获取第一个字符真实位置
int OpenTextFile(char *FileName)
{
    textfile_fd = open(FileName, O_RDONLY);
    if (textfile_fd < 0)
    {
        printf("open %s error\n", FileName);
        return -1;
    }

    struct stat statbuf;
    if (fstat(textfile_fd, &statbuf))
    {
        printf("cannot get stat\n");
        close(textfile_fd);
        return -1;
    }
    
    textfilebuf_start = (unsigned char *)mmap(NULL , statbuf.st_size, PROT_READ, MAP_SHARED, textfile_fd, 0);
    if (textfilebuf_start == (unsigned char *)-1)
    {
        printf("mmap failed\n");
        close(textfile_fd);
        return -1;
    }
    textfilebuf_end = textfilebuf_start + statbuf.st_size;

    // 获取文本文件的编码格式
    textfile_EncodingOpr = GetEncodingFormatForTextFile(textfilebuf_start);

    if (textfile_EncodingOpr)
    {
        printf("Textfile EncodingFormat : %s\n", textfile_EncodingOpr->name);
        textfile_first_char_pos = textfilebuf_start + textfile_EncodingOpr->iHeadLen;

        printf("First Char Position in txt File is : %p\n", textfile_first_char_pos);
        printf("First Char in txt File is : %c\n", *textfile_first_char_pos);
        return 0;
    }
    else
    {
        printf("Get EncodingFormat for Textfile failed!\n");
        return -1;
    }
}

// 设置文本显示的字体
int SetTextFont(char *hzkfile, char *ftfile, unsigned int fontsize)
{
	int iError = 0;
	PFontOpr ptFontOpr;
	int iRet = -1;

	ptFontOpr = textfile_EncodingOpr->ptThisEncodingFormatCanShow;
	while (ptFontOpr)
	{
		if (strcmp(ptFontOpr->name, "ascii") == 0)
		{
			iError = ptFontOpr->FontInit(NULL, textfilt_fontsize);
		}
		else if (strcmp(ptFontOpr->name, "gbk") == 0)
		{
			iError = ptFontOpr->FontInit(hzkfile, textfilt_fontsize);
		}
		else if (strcmp(ptFontOpr->name, "ft") == 0)
		{
			iError = ptFontOpr->FontInit(ftfile, textfilt_fontsize);
		}

		if (iError == 0)
		{
			iRet = 0;
		}
		else
		{
			DelFontOprFrmEncoding(textfile_EncodingOpr, ptFontOpr);
		}
		ptFontOpr = ptFontOpr->ptNext;
	}
	return iRet;
}


int ShowFinalFontForTextFile(void)
{
    PFontOpr ptmp = textfile_EncodingOpr->ptThisEncodingFormatCanShow;
    printf("Final Font to show txt is : %s\n", ptmp->name);
    return 0;
}



// 显示一个字符
int ShowChar(PFontBitMap ptFontBitMap)
{
    int x;
    int y;
    int i = 0;
    int bit;
    unsigned char ucByte = 0;

    if (ptFontBitMap->iBpp == 8)
    {
        for (y = ptFontBitMap->iYTop; y < ptFontBitMap->iYMax; y++)
        {
            for (x = ptFontBitMap->iXLeft; x < ptFontBitMap->iXMax; x++)
            {
                if (ptFontBitMap->pucBuffer[i] != 0)
                {
                    textfile_DispOpr.ShowPixel(x, y, FONT_COLOR);
                    i++;
                }
            }
        }
    }
    else if (ptFontBitMap->iBpp == 1)
    {
        for (y = ptFontBitMap->iYTop; y < ptFontBitMap->iYMax; y++)
        {
            i = (y - ptFontBitMap->iYTop) * ptFontBitMap->iPitch;
            for (x = ptFontBitMap->iXLeft, bit = 7; x < ptFontBitMap->iXMax; x++)
            {
                if (bit == 7)
                {
                    ucByte = ptFontBitMap->pucBuffer[i++];
                }

                if (ucByte & (1 << bit))
                {
                    textfile_DispOpr.ShowPixel(x, y, FONT_COLOR);
                }
                bit--;
                if (bit == -1)
                {
                    bit = 7;
                }
            }
        }
    }

    return 0;
}

